package backtrack;

import java.util.ArrayList;
import java.util.List;

/**
 * 组合问题
 */
public class L77 {

    public static void main(String[] args) {
        System.out.println(combine(4, 2));
    }

    private static final List<List<Integer>> result = new ArrayList<>();

    public static List<List<Integer>> combine(int n, int k) {
        backtracking(n, k, 1, new ArrayList<>());
        return result;
    }

    private static void backtracking(int n, int k, int start, List<Integer> list) {
        if (k == 0) {
            result.add(new ArrayList<>(list));
            return;
        }
        //剪枝
        for (int i = start; i <= n - k + 1; i++) {
            list.add(i);
            backtracking(n, k - 1, i + 1, list);
            list.remove(list.size() - 1);
        }
    }
}
